package com.efast.cafe.portal.service.company.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.efast.cafe.portal.bean.company.PortalCompanyPositionRoleBean;
import com.efast.cafe.portal.service.company.IPortalCompanyPositionRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.jdbc.JdbcDaoImpl;
import com.efast.cafe.framework.jdbc.persistence.Criteria;
import com.efast.cafe.framework.pager.Pagination;
import com.efast.cafe.framework.service.CafeServiceImpl;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.company.PortalCompanyPositionBean;
import com.efast.cafe.portal.bean.company.PortalCompanyPositionQueryForm;
import com.efast.cafe.portal.dao.company.PortalCompanyPositionDao;
import com.efast.cafe.portal.dao.company.PortalCompanyUserReporttoDao;
import com.efast.cafe.portal.entity.company.PortalCompanyOrg;
import com.efast.cafe.portal.entity.company.PortalCompanyPosition;
import com.efast.cafe.portal.entity.company.PortalCompanyUser;
import com.efast.cafe.portal.entity.company.PortalCompanyUserOrg;
import com.efast.cafe.portal.entity.company.PortalCompanyUserReporttoTreeDto;
import com.efast.cafe.portal.service.company.IPortalCompanyPositionService;
import com.github.pagehelper.PageHelper;

@Service
@Transactional
public class PortalCompanyPositionServiceImpl extends CafeServiceImpl<PortalCompanyPosition> implements IPortalCompanyPositionService {

	private PortalCompanyPositionDao portalCompanyPositionDao;

	@Autowired
	private IPortalCompanyPositionRoleService portalCompanyPositionRoleService;

	@Qualifier("jdbcDao")
	@Autowired
	private JdbcDao jdbcDao;
	
	@Autowired
	private PortalCompanyUserReporttoDao portalCompanyUserReporttoDao;

	
	

	@Autowired
	public void setPortalCompanyPositionDao(PortalCompanyPositionDao portalCompanyPositionDao) {
		this.portalCompanyPositionDao = portalCompanyPositionDao;
	}
	
	@Override
	public List<PortalCompanyUserReporttoTreeDto> getCompanyTree() {
		
		List<PortalCompanyUserReporttoTreeDto> list=new ArrayList<PortalCompanyUserReporttoTreeDto>();
		
		Criteria  userCriteria=Criteria.create(PortalCompanyUser.class).where("companyCode", new Object[]{SpringWebUtils.getRequestCompany()});
		List<PortalCompanyUser> listUser= jdbcDao.queryList(userCriteria);
		
		Map<String, PortalCompanyUser> mapUser=new HashMap<String, PortalCompanyUser>();
		for (PortalCompanyUser portalCompanyUser : listUser) {
			mapUser.put(portalCompanyUser.getUsername(), portalCompanyUser);
		}
		
		Criteria orgCriteria=Criteria.create(PortalCompanyOrg.class).where(PortalCompanyOrg.companyCode, new Object[]{SpringWebUtils.getRequestCompany()});
		 List<PortalCompanyOrg> listOrg= jdbcDao.queryList(orgCriteria);
		 for (PortalCompanyOrg portalCompanyOrg : listOrg) {
			 PortalCompanyUserReporttoTreeDto dto = new PortalCompanyUserReporttoTreeDto();
			 dto.setId("org_"+portalCompanyOrg.getPortal_company_org_id());
			 dto.setName(portalCompanyOrg.getOrg_name());
			 dto.setParentId("org_"+portalCompanyOrg.getParent_id());
			 dto.setType("org");
			 dto.setIconSkin("org");
			 dto.setIsHidden(false);
			 list.add(dto);
		}
		 
		 Criteria positionCriteria=Criteria.create(PortalCompanyPosition.class).where(PortalCompanyOrg.companyCode, new Object[]{SpringWebUtils.getRequestCompany()});
		 List<PortalCompanyPosition> listPosition=jdbcDao.queryList(positionCriteria);
		 for (PortalCompanyPosition portalCompanyPosition : listPosition) {
			 PortalCompanyUserReporttoTreeDto dto = new PortalCompanyUserReporttoTreeDto();
			 dto.setId("position_"+portalCompanyPosition.getPortal_company_position_id());
			 dto.setName(portalCompanyPosition.getPosition_name());
			 dto.setParentId("org_"+portalCompanyPosition.getPortal_company_org_id());
			 dto.setType("position");
			 dto.setIsHidden(false);
			 dto.setIconSkin("position");
			 list.add(dto); 
		}
		 
		return list;
	}
	
	
	@Override
	public List<PortalCompanyUserReporttoTreeDto> getLowerList(PortalCompanyUserReporttoTreeDto thisDto) {
		
		List<PortalCompanyUserReporttoTreeDto> list=new ArrayList<PortalCompanyUserReporttoTreeDto>();
		
		Criteria  userCriteria=Criteria.create(PortalCompanyUser.class).where("companyCode", new Object[]{SpringWebUtils.getRequestCompany()});
		List<PortalCompanyUser> listUser= jdbcDao.queryList(userCriteria);
		
		Map<String, PortalCompanyUser> mapUser=new HashMap<String, PortalCompanyUser>();
		for (PortalCompanyUser portalCompanyUser : listUser) {
			mapUser.put(portalCompanyUser.getUsername(), portalCompanyUser);
		}
		
		Criteria orgCriteria=Criteria.create(PortalCompanyOrg.class).where(PortalCompanyOrg.companyCode, new Object[]{SpringWebUtils.getRequestCompany()});
		List<PortalCompanyOrg> listOrg= jdbcDao.queryList(orgCriteria);
		for (PortalCompanyOrg portalCompanyOrg : listOrg) {
			PortalCompanyUserReporttoTreeDto dto = new PortalCompanyUserReporttoTreeDto();
			dto.setId("org_"+portalCompanyOrg.getPortal_company_org_id());
			dto.setName(portalCompanyOrg.getOrg_name());
			dto.setParentId("org_"+portalCompanyOrg.getParent_id());
			dto.setType("org");
			dto.setIconSkin("org");
			dto.setIsHidden(false);
			list.add(dto);
		}
		
		 Criteria positionCriteria=Criteria.create(PortalCompanyPosition.class).where(PortalCompanyOrg.companyCode, new Object[]{SpringWebUtils.getRequestCompany()});
		 List<PortalCompanyPosition> listPosition=jdbcDao.queryList(positionCriteria);
		 for (PortalCompanyPosition portalCompanyPosition : listPosition) {
			 PortalCompanyUserReporttoTreeDto dto = new PortalCompanyUserReporttoTreeDto();
			 dto.setId("position_"+portalCompanyPosition.getPortal_company_position_id());
			 dto.setName(portalCompanyPosition.getPosition_name());
			 dto.setParentId("org_"+portalCompanyPosition.getPortal_company_org_id());
			 dto.setType("position");
			 dto.setIsHidden(false);
			 dto.setIconSkin("position");
			 list.add(dto); 
		 }
	 
		 List<PortalCompanyUserOrg> listUserOrg=portalCompanyUserReporttoDao.queryUserOrgList();
		 for (PortalCompanyUserOrg portalCompanyUserOrg : listUserOrg) {
			 PortalCompanyUserReporttoTreeDto user = new PortalCompanyUserReporttoTreeDto();
			 user.setId("user_"+portalCompanyUserOrg.getUsername()+"_"+portalCompanyUserOrg.getPortal_company_user_org_id());
			 user.setName(portalCompanyUserOrg.getUsername());
			 user.setParentId("position_"+portalCompanyUserOrg.getPortal_company_position_id());
			 user.setType("user");
			 user.setIsHidden(false);
			 user.setIconSkin("user");
			 list.add(user);
		 }
		 
		 List<PortalCompanyUserReporttoTreeDto> listR=new ArrayList<PortalCompanyUserReporttoTreeDto>();
		 
		 if("company".equals(thisDto.getType())){
			 listR = list;
		 }else if("org".equals(thisDto.getType())){
			 List<String> ids = new ArrayList<String>();
			 for(PortalCompanyUserReporttoTreeDto dto : list){
				 if(thisDto.getId().equals(dto.getParentId())){
					 listR.add(dto);
					 ids.add(dto.getId());
				 }
			 }
			 while(ids.size()>0){
				 List<String> thisId = new ArrayList<String>();
				 for(String i : ids){
					 thisId.add(i);
				 }
				 ids.clear();
				 for(PortalCompanyUserReporttoTreeDto dto : list){
					 if(thisId.contains(dto.getParentId())){
						 listR.add(dto);
						 ids.add(dto.getId());
					 }
				 }				 
			 }
		 }else if("position".equals(thisDto.getType())){
			 for(PortalCompanyUserReporttoTreeDto dto : list){
				 if(thisDto.getId().equals(dto.getParentId())){
					 listR.add(dto);
				 }
			 }
		 }
//		 if("user".equals(thisDto.getLevel())||"".equals(thisDto.getLevel())||thisDto.getLevel()==null){
		 
		 List<PortalCompanyUserReporttoTreeDto> listReturn=new ArrayList<PortalCompanyUserReporttoTreeDto>();
		 if(thisDto.getLevel()==null||"".equals(thisDto.getLevel())){
			 listReturn=listR;
		 }else{
			 for(PortalCompanyUserReporttoTreeDto dto : listR){
				 if(thisDto.getLevel().equals(dto.getType())){
					 listReturn.add(dto); 
				 }
			 }
		 }
		 
		return listReturn;
	}
	
	

	@Override
	public List<PortalCompanyPosition> queryPositionByOrgId(Long portalCompanyOrgId) {
		String sql = "select p.*,o.org_code from portal_company_org o,portal_company_position p where o.portal_company_org_id = p.portal_company_org_id and p.company_code = ? and p.portal_company_org_id = ?";
		List<Object> params = new ArrayList<Object>();
		params.add(SpringWebUtils.getRequestCompany());
		params.add(portalCompanyOrgId);
		return jdbcDao.queryBySQL(sql, params, PortalCompanyPosition.class);
	}

	@Override
	public Pagination<PortalCompanyPositionBean> querylistPosition(
			PortalCompanyPositionQueryForm portalCompanyPosition) {
		portalCompanyPosition.setCompany_code(SpringWebUtils.getRequestCompany());
		PageHelper.startPage(portalCompanyPosition.getStart(), portalCompanyPosition.getLength());
		List<PortalCompanyPositionBean> list = portalCompanyPositionDao.querylistPosition(portalCompanyPosition);
		int count = portalCompanyPositionDao.queryCount(portalCompanyPosition);
		Pagination<PortalCompanyPositionBean> pager = new Pagination<PortalCompanyPositionBean>();
		pager.setResultList(list);
		pager.setTotalRows(count);
		return pager;

	}

	@Override
	public void delPosutionById(Long portalCompanyPositionId) {
		// portalCompanyPositionDao.delete(Criteria.create(PortalCompanyPosition.class).where("","portalCompanyOrgId",
		// new Object[]{portalCompanyOrgId}));
		jdbcDao.delete(PortalCompanyPosition.class, portalCompanyPositionId);
	}

	@Override
	public PortalCompanyPosition queryPositionById(Long portalCompanyPositionId) {
		return portalCompanyPositionDao.queryPositionById(portalCompanyPositionId);
	}

	@Override
	public void updatePosition(PortalCompanyPositionRoleBean portalCompanyPositionRoleBean) {
		PortalCompanyPosition portalCompanyPosition = portalCompanyPositionRoleBean.getPortalCompanyPosition() ;
		int count = portalCompanyPositionDao.orgCodeIsExistWithId(portalCompanyPosition.getPortal_company_position_id(),
				portalCompanyPosition.getPosition_code(), SpringWebUtils.getRequestCompany());
		if (count != 0) {
			throw new ServiceException("msg.portal.company.org.exist");// org已经存在
		}
		jdbcDao.update(Criteria.create(PortalCompanyPosition.class)
				.where("portal_company_position_id",
						new Object[] { portalCompanyPosition.getPortal_company_position_id() })
				.set("position_name", portalCompanyPosition.getPosition_name())
				.set("position_code", portalCompanyPosition.getPosition_code())
				.set("position_desc", portalCompanyPosition.getPosition_desc())
				.and("company_code", new Object[] { SpringWebUtils.getRequestCompany() }));
		Long [] roles = (Long []) portalCompanyPositionRoleBean.getRoles().toArray(new Long[portalCompanyPositionRoleBean.getRoles().size()]);
		portalCompanyPositionRoleService.savePositionRoleMultiple(portalCompanyPosition.getPortal_company_position_id(),roles);
	}

	@Override
	public Long insertPosition(PortalCompanyPositionRoleBean portalCompanyPositionRoleBean) {
		PortalCompanyPosition portalCompanyPosition = portalCompanyPositionRoleBean.getPortalCompanyPosition() ;
		if(portalCompanyPosition.getPortal_company_org_id()==null){
			portalCompanyPosition.setPortal_company_org_id(0L);
		}
		Criteria criteria = Criteria.create(PortalCompanyPosition.class)
				.where("position_code", new Object[] { portalCompanyPosition.getPosition_code() })
				.and("company_code", new Object[] { SpringWebUtils.getRequestCompany() });
		int count = jdbcDao.queryCount(criteria);
		if (count != 0) {
			throw new ServiceException("msg.portal.company.org.exist");// org已经存在
		}
		long id = jdbcDao.insert(Criteria.create(PortalCompanyPosition.class)
				.set("position_name", portalCompanyPosition.getPosition_name())
				.set("position_code", portalCompanyPosition.getPosition_code())
				.set("position_desc", portalCompanyPosition.getPosition_desc())
				.set("company_code", SpringWebUtils.getRequestCompany())
				.set("parent_id", portalCompanyPosition.getParentId())
				.set("portal_company_org_id", portalCompanyPosition.getPortal_company_org_id()));
		Long [] roles = (Long []) portalCompanyPositionRoleBean.getRoles().toArray(new Long[portalCompanyPositionRoleBean.getRoles().size()]);
		portalCompanyPositionRoleService.savePositionRoleMultiple(id,roles);
		return id;
	}

	@Override
	public int listUserByPositionId(Long portalCompanyPositionId) {
		Criteria criteria = Criteria.create(PortalCompanyUserOrg.class)
				.where(PortalCompanyUserOrg.portalCompanyPositionId, new Object[] { portalCompanyPositionId });
		return jdbcDao.queryCount(criteria);
	}

	@Override
	public PortalCompanyPosition getCurrentUserPosition() {
		PortalCompanyPosition portalCompanyPosition = portalCompanyPositionDao
				.getCurrentUserPosition(SpringWebUtils.getRequestUser(), SpringWebUtils.getRequestCompany());
		return portalCompanyPosition;
	}

	/**
	 * 根据组织id查询职位
	 * @param orgId  null则查询所有职位
	 * @return List<PortalCompanyPosition>
	 * @Author Administrator
	 * @Date 2017年9月26日
	 */
	@Override
	public List<PortalCompanyPositionBean> queryPositionOrgId(Integer orgId) {

		return portalCompanyPositionDao.queryAllPosition(orgId, SpringWebUtils.getRequestCompany());
	}

}
